﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;

namespace PublicGateway.Host.Controllers
{
    /// <summary>
    /// 资源控制器
    /// </summary>
    [RemoteService]
    [Area("commonManagement")]
    [Route("api/commonManagement/resources")]
    public class ResourcesController : AbpController
    {
        private readonly static string errorImage = "";

        static ResourcesController()
        {
            errorImage = $"{Directory.GetCurrentDirectory()}/images/404.jpg";
        }

        // 图片
        [HttpGet]
        [Route("image/{name}")]
        public IActionResult Image(string name)
        {
            // 获取图片的返回类型
            var contentTypeDict = new Dictionary<string, string> {
                {"jpg","image/jpeg"},
                {"jpeg","image/jpeg"},
                {"jpe","image/jpeg"},
                {"png","image/png"},
                {"gif","image/gif"},
                {"ico","image/x-ico"},
                {"tif","image/tiff"},
                {"tiff","image/tiff"},
                {"fax","image/fax"},
                {"wbmp","image/nd.wap.wbmp"},
                {"rp","imagend.rn-realpix"}
            };
            string imgPath, contentType = string.Empty;
            var imgTypeSplit = name.Split('.');
            var imgType = imgTypeSplit.Last().ToLower();
            // 未知的图片类型
            if (!contentTypeDict.ContainsKey(imgType))
            {
                imgPath = errorImage;
            }
            else
            {
                contentType = contentTypeDict[imgType];
                // 图片不存在
                imgPath = $"{Directory.GetCurrentDirectory()}\\Images\\{name}";
                if (!new FileInfo(imgPath).Exists)
                    imgPath = errorImage;
            }
            // 读取图片
            using (var fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
            {
                var bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                return new FileContentResult(bytes, contentType);
            }
        }

        // 图标
        [HttpGet]
        [Route("icon/{name}")]
        public IActionResult Icon(string name)
        {
            // 获取图片的返回类型
            var contentTypeDict = new Dictionary<string, string> {
                {"jpg","image/jpeg"},
                {"jpeg","image/jpeg"},
                {"jpe","image/jpeg"},
                {"png","image/png"},
                {"gif","image/gif"},
                {"ico","image/x-ico"},
                {"tif","image/tiff"},
                {"tiff","image/tiff"},
                {"fax","image/fax"},
                {"wbmp","image/nd.wap.wbmp"},
                {"rp","imagend.rn-realpix"}
            };
            string imgPath, contentType = string.Empty;
            var imgTypeSplit = name.Split('.');
            var imgType = imgTypeSplit.Last().ToLower();
            // 未知的图片类型
            if (!contentTypeDict.ContainsKey(imgType))
            {
                imgPath = errorImage;
            }
            else
            {
                contentType = contentTypeDict[imgType];
                // 图片不存在
                imgPath = $"{Directory.GetCurrentDirectory()}\\Icons\\{name}";
                if (!new FileInfo(imgPath).Exists)
                    imgPath = errorImage;
            }
            // 读取图片
            using (var fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
            {
                var bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                return new FileContentResult(bytes, contentType);
            }
        }
    }
}